package com.ruoyi.web.controller.open;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.open.domain.Client;
import com.ruoyi.open.service.ClientService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 开放平台Controller
 *
 * @author anan
 * @date 2021-04-02
 */
@RestController
@RequestMapping("/system/client")
public class ClientController extends BaseController
{
    @Autowired
    private ClientService clientService;

    /**
     * 查询开放平台列表
     */
    @PreAuthorize("@ss.hasPermi('system:client:list')")
    @GetMapping("/list")
    public TableDataInfo list(Client client)
    {
		PagePlus<Client, Client> clientClientPagePlus = clientService.pageVo(PageUtils.buildPagePlus(), Client.class);
		return PageUtils.buildDataInfo(clientClientPagePlus);
    }

    /**
     * 导出开放平台列表
     */
    @PreAuthorize("@ss.hasPermi('system:client:export')")
    @Log(title = "开放平台", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Client client)
    {
        List<Client> list = clientService.selectClientList(client);
        ExcelUtil<Client> util = new ExcelUtil<Client>(Client.class);
        return util.exportExcel(list, "client");
    }

    /**
     * 获取开放平台详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:client:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(clientService.getById(id));
    }

    /**
     * 新增开放平台
     */
    @PreAuthorize("@ss.hasPermi('system:client:add')")
    @Log(title = "开放平台", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Client client)
    {
		return toAjax(clientService.save(client));
    }

    /**
     * 修改开放平台
     */
    @PreAuthorize("@ss.hasPermi('system:client:edit')")
    @Log(title = "开放平台", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Client client)
    {
        return toAjax(clientService.updateById(client));
    }

    /**
     * 删除开放平台
     */
    @PreAuthorize("@ss.hasPermi('system:client:remove')")
    @Log(title = "开放平台", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
		List list = CollectionUtils.arrayToList(ids);
        return toAjax(clientService.removeByIds(list));
    }

    /**
     * 生成随机secretKey
     * @return
     */
    @ApiOperation(value = "生成随机secretKey")
    @GetMapping(value = "/getSecretKey")
    public AjaxResult getSecretKey(){
        Map<String, String> map = new HashMap<>(16);
        map.put("clientSecret",UUID.randomUUID().toString().replaceAll("-", ""));
        return AjaxResult.success(map);
    }
}
